liwe.cookie = {};

// {{{{ base docs
/**
Cookie
======

It is very easy to handle cookies with the LiWE library.

**/
// }}}

// {{{ liwe.cookie.get_exp_date ( days, hours, minutes )
/**
.. function:: liwe.cookie.get_exp_date ( days, hours, minutes )

	Calculates expiring date from ``days``, ``hours`` and ``minutes``.
	The result of this function is a string containing the expiring date in ``GMT`` format.

	.. note:: 
		it is possible to pass negative values for numbers

	:param days: number of days for the cookie before expire.
	:param hours: number of hours for the cookie before expire.
	:param minutes: number of minutes for the cookie before expire.

	:rtype: a string with the expiring date in ``GTM`` format.

**/
liwe.cookie.get_exp_date = function ( days, hours, minutes )
{
	var exp_date = new Date ();

	exp_date.setDate ( exp_date.getDate () + parseInt ( days, 10 ) );
	exp_date.setHours ( exp_date.getHours () + parseInt ( hours, 10 ) );
	exp_date.setMinutes ( exp_date.getMinutes () + parseInt ( minutes, 10 ) );

	return exp_date.toGMTString ();
};
// }}}
// {{{ liwe.cookie.del ( name, [ path, [ domain ] ] )
/**
.. function:: liwe.cookie.del ( name, [ path, [ domain ] ] )

	This method deletes a cookie from the browser (it makes it expire).

	:param name: the name of the cookie to delete.
	:param path: the path of the cookie (optional).
	:param domain: the cookie domain (optional)

	:rtype: this function returns nothing.

**/
liwe.cookie.del = function ( name, path, domain )
{
	if ( this.get ( name ) )
	{
		document.cookie = name + "=" + 
			( ( path ) ? 	"; path=" + path : "" ) +
		 	( ( domain ) ? 	"; domain=" + domain : "" ) +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
};
// }}}
// {{{ liwe.cookie.get ( name )
/**
.. function:: liwe.cookie.get ( name )

	Returns the value of the cookie recognized by ``name``.

	:param name: cookie name

	:rtype:	a string containing the cookie value
**/
liwe.cookie.get = function ( name )
{
	var arg = name + "=";
	var alen = arg.length;	
	var clen = document.cookie.length;
	var i = 0;

	while ( i < clen )
	{
		var j = i + alen;
		if ( document.cookie.substring ( i, j ) == arg ) return liwe.cookie._get_val ( j );

		i = document.cookie.indexOf ( " ", i ) + 1;
		if ( i == 0 ) break;
	}

	// Se sono arrivato fino a qui, non ho trovato il cookie ``name`` e quindi
	// restituisco stringa vuota.
	return "";
};
// }}}
// {{{ liwe.cookie.set ( name, value, [ expires, [ path, [ domain, [ secure ] ] ] ] )
/**
.. function:: liwe.cookie.set ( name, value, [ expires, [ path, [ domain, [ secure ] ] ] ] )

	Set a new cookie in the browser.

	:param name: the cookie name.
	:param value: the cookie value.
	:param expires: the expiration date (optional)
	:param path: the path where cookie is valid (optional)
	:param domain: the domanin where cookie is valid (optional)
	:param secure: if ``secure`` is ``true``, the cookie will be secure.
**/
liwe.cookie.set = function ( name, value, expires, path, domain, secure )
{
	document.cookie = name + "=" + escape ( value ) + 
		( ( expires ) ? "; expires=" + expires : "" ) +
		( ( path )    ? "; path=" + path : "" ) +
		( ( domain )  ? "; domain=" + domain : "" ) +
		( ( secure )  ? "; secure" : "" );
};
// }}}

/* =====================================================================
   FUNZIONI INTERNE
 ==================================================================== */

// {{{ liwe.cookie._get_val ( offset )
liwe.cookie._get_val = function ( offset )
{
	var end_str = document.cookie.indexOf ( ";", offset );

	if ( end_str == -1 ) end_str = document.cookie.length;

	return unescape ( document.cookie.substring ( offset, end_str ) );
};
// }}}
